//
//  FightGroups.m
//  KYRR
//
//  Created by kyjun on 16/6/6.
//
//

#import "FightGroups.h"
#import "FightGroupCell.h"
#import "FightGroupItem.h"
#import "FightGroup.h"

@interface FightGroups ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property(nonatomic,copy) NSString* cellIdentifier;
@property(nonatomic,strong) NetPage* page;
@property(nonatomic,strong) NSMutableArray* arrayData;

@end

@implementation FightGroups


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.cellIdentifier = @"FightGroupCell";
    self.automaticallyAdjustsScrollViewInsets = YES;
    [self layoutUI];
    [self refreshDataSource];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFightGroupCell:) name:NotificationrefReshFightGroupCell object:nil];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationItem.title = @"拼团购";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)layoutUI{
    [self.tableView registerClass:[FightGroupCell class] forCellReuseIdentifier:self.cellIdentifier];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
}

#pragma mark =====================================================  Data Source
-(void)queryData{
    NetRepositories* repositories = [[NetRepositories alloc]init];
    [repositories queryFightGroups:@{@"ince":@"get_pintuan_list",@"zone_id":self.Identity.location.circleID,@"page":[WMHelper integerConvertToString:self.page.pageIndex]} page:self.page complte:^(NSInteger react, NSArray *list, NSString *message) {
        if(react == 1){
            if(self.page.pageIndex == 1)
                [self.arrayData removeAllObjects];
            [list enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.arrayData addObject:obj];
            }];
        }else if(react == 400){
            [self alertHUD:message];
        }else{
            [self alertHUD:message];
        }
        [self.tableView reloadData];
        if(self.page.pageCount<=self.page.pageIndex || list.count<self.page.pageSize){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if(self.page.pageIndex==1){
            [self.tableView.mj_header endRefreshing];
        }
        
    }];
    
}

-(void)refreshDataSource{
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page.pageIndex = 1;
        [weakSelf queryData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        weakSelf.page.pageIndex ++;
        [weakSelf queryData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark =====================================================  <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FightGroupCell* cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    MFightGroup* item = self.arrayData[indexPath.row];
    cell.entity = item;
    cell.tag = indexPath.row;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
}

#pragma mark =====================================================  <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   /* MFightGroup* item = self.arrayData[indexPath.row];
    if(CGSizeEqualToSize(item.thumbnailsSize, CGSizeZero)){
        return 95+200.f;
    }else{
        return 95+item.thumbnailsSize.height;
    }*/
    return 90+SCREEN_WIDTH*7/15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MFightGroup* item = self.arrayData[indexPath.row];
   // FightGroupInfo* controller = [[FightGroupInfo alloc]initWithRowID:item.rowID];
    
   // FightGroupItem* controller = [[FightGroupItem alloc]initWithRowID:item.rowID];
    FightGroup* controller = [[FightGroup alloc]initWithRowID:item.rowID];
    [self.navigationController pushViewController:controller animated:YES];
}


#pragma mark =====================================================  DZEmptyData 协议实现

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    return  [[NSMutableAttributedString alloc] initWithString:@"没有查询到相关数据" attributes:@{NSForegroundColorAttributeName:[UIColor grayColor],NSParagraphStyleAttributeName:paragraph}];
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:kDefaultImage];
}
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return self.tableView.tableHeaderView.frame.size.height/10.0f;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}
#pragma mark =====================================================  Notification
-(void)refreshFightGroupCell:(NSNotification*)notification{
    NSIndexPath* indexPath = notification.object;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark =====================================================  property package
-(NSMutableArray *)arrayData{
    if(!_arrayData){
        _arrayData = [[NSMutableArray alloc]init];
    }
    return _arrayData;
}

-(NetPage *)page{
    if(!_page){
        _page = [[NetPage alloc]init];
        _page.pageIndex = 1;
    }
    return _page;
}

@end
